﻿using CommunityToolkit.Mvvm.ComponentModel;

namespace TotalApplication.Core
{
    /// <summary>
    /// The application state of a view model
    /// </summary>
    public partial class ApplicationViewModel : BaseViewModel
    {
        private ApplicationPage _currentPage = ApplicationPage.Chat;
        private BaseViewModel? _currentPageViewModel;

        #region Public Properties

        /// <summary>
        /// The current page of application (private setter)
        /// </summary>
        public ApplicationPage CurrentPage
        {
            get => _currentPage;
            private set => SetProperty(ref _currentPage, value);
        }

        /// <summary>
        /// The current page's view model of application (private setter)
        /// </summary>
        public BaseViewModel CurrentPageViewModel
        {
            get => _currentPageViewModel ?? new BaseViewModel();
            private set => SetProperty(ref _currentPageViewModel, value);
        }

        /// <summary>
        /// True if the side menu should be shown
        /// </summary>
        [ObservableProperty]
        private bool _sideMenuVisible = true;

        /// <summary>
        /// True if the setting menu should be shown
        /// </summary>
        [ObservableProperty]
        private bool _SettingMenuVisible = false;

        #endregion

        /// <summary>
        /// Navigates to the specified page
        /// </summary>
        /// <param name="page">The page to go to</param>
        /// <exception cref="NotImplementedException"></exception>
        public void GoToPage(ApplicationPage page)
        {
            // Always hide setting menu when changing page
            SettingMenuVisible = false;

            // Set current page
            CurrentPage = page;

            // Fire off a CurrentPage event
            OnPropertyChanged(nameof(CurrentPage));

            // Boolean sideMenuVisibility
            SideMenuVisible = page == ApplicationPage.Chat;
        }

        /// <summary>
        /// Navigates to the specified page
        /// </summary>
        /// <param name="page">The page to go to</param>
        /// <exception cref="NotImplementedException"></exception>
        public void GoToPage(ApplicationPage page, BaseViewModel? viewModel = null)
        {
            // Always hide setting menu when changing page
            SettingMenuVisible = false;

            // Set the view model
            CurrentPageViewModel = viewModel == null ? new BaseViewModel() : viewModel;

            // Set current page
            CurrentPage = page;

            // Fire off a CurrentPage event
            OnPropertyChanged(nameof(CurrentPage));

            // Boolean sideMenuVisibility
            SideMenuVisible = page == ApplicationPage.Chat;
        }
    }
}
